package basicobject

import (
	"yizhuoyan/go-study/pdfgo/handyio"
)

type DictionaryOrArray struct {
	Dictionary *Dictionary
	Array      *Array
}

func (this *DictionaryOrArray) Serialize(w handyio.PdfRawWriter) error {
	if this.Dictionary != nil {
		return this.Dictionary.Serialize(w)
	}
	return this.Array.Serialize(w)
}

func (this *DictionaryOrArray) UnSerialize(r handyio.PdfRawReader) error {
	startAt := r.IndexCurrent()
	if r.NextByteEquals('[') {
		this.Array = new(Array)
		if err := r.IndexMoveTo(r.IndexCurrent() - 1); err != nil {
			return err
		}
		if err := this.Array.UnSerialize(r); err != nil {
			_ = r.IndexMoveTo(startAt)
			return err
		}
	}
	this.Dictionary = new(Dictionary)
	if err := this.Dictionary.UnSerialize(r); err != nil {
		_ = r.IndexMoveTo(startAt)
		return err
	}
	return nil
}
